package com.wy.ssm01.controller;


import com.alibaba.druid.support.json.JSONUtils;
import com.google.gson.Gson;
import com.wy.ssm01.entity.MyTestEntity;
import com.wy.ssm01.exception_zdy.MyException;
import com.wy.ssm01.service.TestService;
import com.wy.ssm01.utils.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class TestController01 {

    @Autowired
    TestService testService;
    @RequestMapping("/")
    public String a() {

        return "testList";
    }

    @RequestMapping("/test2")
    public String b() throws Exception {
        throw new MyException("哈哈哈出错了hehehe");
    }

    @RequestMapping("/getImg")
    public void getImg(HttpServletResponse response, String name) throws Exception {
        Random r = new Random();
        File file = new File("D:\\桌面文件\\壁纸\\壁纸 csxy\\csxygqdnbz\\1 (" + r.nextInt(100) + ").jpg");
        while (!file.exists()){
            file = new File("D:\\桌面文件\\壁纸\\壁纸 csxy\\csxygqdnbz\\1 (" + r.nextInt(100) + ").jpg");
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream outputStream = response.getOutputStream();
        byte[] bytes = new byte[1024 * 1024 * 2];
        int length;
        while ((length = fileInputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, length);
        }

    }

    @RequestMapping("/getImgs")
    public String getImgs() throws Exception {

        return "getImgs";

    }

    @RequestMapping("/showTests")
    public ModelAndView showTests() throws Exception {
        List<MyTestEntity> myTestEntities = testService.queryAll();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("testList");
        mav.addObject("list",myTestEntities);
        return mav;
    }

    @RequestMapping("/toLogin")
    public String toLogin() throws Exception {
        return "login";
    }

    @RequestMapping("/toRegister")
    public String toRegister() throws Exception {
        return "register";
    }

    @RequestMapping("/login")
    public String login(String username,String password) throws Exception {
        
        return "register";
    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(String username,String password) throws Exception {
        MyTestEntity myTestEntity = new MyTestEntity(null, username, password, new Date(), new BigDecimal("30"));
        int insert = testService.insert(myTestEntity);
        Gson gson = new Gson();
        if (insert>0){
            return gson.toJson(ResultBean.getInstance(ResultBean.OPERATION_SUCCESS,"/SSM_01/login","注册成功"));
        }else {
            return gson.toJson(ResultBean.getInstance(ResultBean.OPERATION_FAILED,"注册失败"));
        }
    }

}
